<?php
class excel {

	public function getSheet(){
 		include (dirname(dirname(dirname(__FILE__))).'/lib/PHPExcel/PHPExcel.class.php');
 		include (dirname(dirname(dirname(__FILE__))).'/lib/PHPExcel/PHPExcel/Reader/Excel2007.php');
 		include (dirname(dirname(dirname(__FILE__))).'/lib/PHPExcel/PHPExcel/Reader/Excel5.php');
 		include (dirname(dirname(dirname(__FILE__))).'/lib/PHPExcel/PHPExcel/IOFactory.php');
		
		//创建一个execl
		$objExcel = new PHPExcel();
		//设置属性 (这段代码无关紧要，内容可以替换)
		$objExcel->getProperties()->setCreator("GuoLC");
		$objExcel->getProperties()->setLastModifiedBy("GuoLC");
		$objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
		$objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
		$objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
		$objExcel->getProperties()->setKeywords("office 2003 openxml php");
		$objExcel->getProperties()->setCategory("Test result file");
		$objExcel->setActiveSheetIndex(0);
			
		return $objExcel;
	}
	
	public function export($objExcel,$sheet,$fName){
		$sheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
		$sheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
			
		$timestamp = time();
		$ex='2003';
		$today=date("Ymd");
	
		header("Content-Type: application/force-download");
		header("Content-Type: application/octet-stream");
		header("Content-Type: application/download");
		if($ex == '2007') { //导出excel2007文档
				
			header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
			header('Content-Disposition: attachment;filename="'.$fName.$today.'.xlsx"');
			header('Cache-Control: max-age=0');
			$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
			$objWriter->save('php://output');
			exit;
				
		} else {  //导出excel2003文档
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="'.$fName.$today.'.xls"');
			header("Content-Transfer-Encoding: binary");
			header('Cache-Control: max-age=0');
			$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
			header("Pragma: no-cache");
			$objWriter->save('php://output');
			exit;
		}
	}
	
	//批量设置单元格格式
	public function setCellFormat($sheet,$startCell,$endCell){
		
		$styleArray = array(
				'borders' => array(
						'allborders' => array(
								//'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
								'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框
								'color' => array('argb' => 'FF000000')
						)
				),
				'alignment' => array(
						'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
						'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
						'wrap'       => true
				)
		);
		
		$sheet->getStyle("$startCell:$endCell")->applyFromArray($styleArray);
		//$sheet->getStyle("$startCell:$endCell")->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
		
	}
}